package common.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;

public class Configs {
    private Map<String, Properties> mConfigCash = CachedMap.getMap(Configs.class);

    private static Configs CONFIGS = null;

    private final static Logger logger = Logger.getLogger(Configs.class);

    private static String mConfigFileName = getFilePath("config.properties", Configs.class);

    // 是否缓存配置文件的开关
    private boolean mCashFlag = false;

    private Configs() {
        Properties tConfig = getNewConfig(mConfigFileName);
        if (tConfig != null) {
            mConfigCash.put(mConfigFileName, tConfig);
            mCashFlag = "true".equals(tConfig.getProperty("CashFlag"));
            logger.info("配置文件缓存开关：" + mCashFlag);
        }
    }

    private static Configs getConfigs() {
        if (CONFIGS == null) {
            CONFIGS = SingtonUtil.getInstance(Configs.class);
        }
        return CONFIGS;
    }

    public static Properties getConfig(String cFileName) {
        Configs tConfigs = Configs.getConfigs();
        Properties tConfig = tConfigs.mConfigCash.get(cFileName);
        if (tConfig == CachedMap.NULL_VALUE) {
            return null;
        }
        if (!tConfigs.mCashFlag || tConfig == null) {
            tConfig = tConfigs.getNewConfig(cFileName);
            if (tConfig == null) {
                CachedMap.addNull(Configs.class, cFileName);
            } else {
                tConfigs.mConfigCash.put(cFileName, tConfig);
            }
        }
        return tConfig;
    }

    private Properties getNewConfig(String cFileName) {
        Properties tConfig = null;
        try {
            InputStream inStream = Configs.class.getClassLoader().getResourceAsStream(cFileName);
            if(inStream == null)
            {
                return null;
            }
            tConfig = new Properties();
            tConfig.load(inStream);
        } catch (IOException e) {
            e.printStackTrace();
            logger.error(cFileName + "加载系统配置文件失败", e);
        }
        return tConfig;
    }

    public static String getProperty(String cFileName, String cKey) {
        Properties tConfig = getConfig(cFileName);
        if (tConfig == null) {
            return null;
        }
        String tValue = tConfig.getProperty(cKey);
        return tValue;
    }

    public static String getProperty(String cFileName, String cKey, String cDefaultValue) {
        String tValue = getProperty(cFileName, cKey);
        return (tValue == null) ? cDefaultValue : tValue;
    }

    public static String getFilePath(String cFileName, Class<?> cCls) {
        String cFilePath = cCls.getPackage().getName().replace(".", File.separator) + File.separator + cFileName;
        return cFilePath;
    }

    public static void refresh() {
        SingtonUtil.newInstance(Configs.class);
    }

    public static void main(String[] args) {
        getConfigs();
    }
}
